package com.xmb.wechat.view.wechat.money;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.definterface.CommonIAndSListener;
import com.xmb.wechat.definterface.HintListener;
import com.xmb.wechat.definterface.OnChooseMeListener;
import com.xmb.wechat.definterface.SimpleTextWatcher;
import com.xmb.wechat.entity.PaySuccessEntity;
import com.xmb.wechat.helper.DataHelper;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/15 08:51
 */
public class WechatFundPaySucActivity extends BaseActivity {

    @BindView(R2.id.btn_save)
    Button         mBtnSave;
    @BindView(R2.id.et_money)
    EditText       mEtMoney;
    @BindView(R2.id.et_name)
    EditText       mEtName;
    @BindView(R2.id.ll_gzh)
    LinearLayout   mLlGZH;
    @BindView(R2.id.switch_gzh)
    Switch         mSwitchGzh;
    @BindView(R2.id.et_gzh_name)
    EditText       mEtGzhName;
    @BindView(R2.id.tv_type)
    TextView       mTvType;
    @BindView(R2.id.re_receiver)
    RelativeLayout mReReceiver;
    @BindView(R2.id.iv_receiver)
    ImageView      mIvReceiver;
    @BindView(R2.id.tv_receiver)
    TextView       mTvReceiver;

    private PaySuccessEntity  mPaySuccessEntity;
    private WechatContactBean mWechatContactBean;

    public WechatFundPaySucActivity() {
        super(R.layout.activity_fund_pay_suc);
    }

    @Override
    protected void initView() {
        mPaySuccessEntity = new PaySuccessEntity();
        mWechatContactBean = new WechatContactBean();
        mSwitchGzh.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEtGzhName.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                mPaySuccessEntity.setAttention(isChecked);
                checkIsAllSatisfy();
            }
        });
        mEtName.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                mPaySuccessEntity.setName(value);
                checkIsAllSatisfy();
            }
        });
        mEtMoney.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                mPaySuccessEntity.setMoney(value);
                checkIsAllSatisfy();
            }
        });
        mEtGzhName.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                mPaySuccessEntity.setGzhName(value);
                checkIsAllSatisfy();
            }
        });
        showExemption(new HintListener() {
            @Override
            public void onConfirm() {
                //do nothing
            }

            @Override
            public void onCancel() {
                WechatFundPaySucActivity.this.finish();
            }
        });
    }

    @OnClick({R2.id.btn_save, R2.id.ll_type, R2.id.re_receiver})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.btn_save) {
            WechatFundPaySucDetailActivity.start(WechatFundPaySucActivity.this, mPaySuccessEntity, mWechatContactBean);
        } else if (id == R.id.ll_type) {
            showListDialog(DataHelper.getPayTypeData(), new CommonIAndSListener() {
                @Override
                public void onChoose(int i, String s) {
                    switch (i) {
                        case 0:
                            mReReceiver.setVisibility(View.GONE);
                            mEtName.setHint("设置商户名称");
                            mLlGZH.setVisibility(View.VISIBLE);
                            mEtGzhName.setVisibility(mSwitchGzh.isChecked() ? View.VISIBLE : View.GONE);
                            mPaySuccessEntity.setPayType(2);
                            break;
                        case 1:
                            mReReceiver.setVisibility(View.VISIBLE);
                            mEtName.setHint("收款人真实姓名");
                            mLlGZH.setVisibility(View.GONE);
                            mEtGzhName.setVisibility(View.GONE);
                            mPaySuccessEntity.setPayType(1);
                            break;
                        case 2:
                            mReReceiver.setVisibility(View.GONE);
                            mEtName.setHint("收款人真实姓名");
                            mLlGZH.setVisibility(View.GONE);
                            mEtGzhName.setVisibility(View.GONE);
                            mPaySuccessEntity.setPayType(0);
                            break;
                    }
                    mTvType.setText(s);
                    checkIsAllSatisfy();
                }
            });
        } else if (id == R.id.re_receiver) {
            showRoleChooseReceiver(this, true, true, new OnChooseMeListener() {
                @Override
                public void onMeChoose(WechatContactBean mineContactBean) {
                    checkAndInitValue(mineContactBean);
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE_OTHER_RECEIVE || requestCode == REQUEST_CODE_CHOOSE_NEW_ADD_RECEIVE) {
            if (data == null)
                return;
            WechatContactBean result = (WechatContactBean) data.getSerializableExtra("result");
            checkAndInitValue(result);
        }
    }

    private void checkAndInitValue(WechatContactBean result) {
        if (result != null) {
            mWechatContactBean = result;
            //显示界面
            WechatContactBean.setupAvatarIntoImageView(mWechatContactBean, mIvReceiver, 12);
            mTvReceiver.setText(TextUtils.isEmpty(mWechatContactBean.getName()) ? "" : mWechatContactBean.getName());
        }
        checkIsAllSatisfy();
    }

    /**
     * 检查各条件是否具备(按钮是否可置able)
     */
    private void checkIsAllSatisfy() {
        String name = mEtName.getText().toString().trim();
        String money = mEtMoney.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            mBtnSave.setEnabled(false);
            return;
        }
        if (TextUtils.isEmpty(money)) {
            mBtnSave.setEnabled(false);
            return;
        }
        if (mPaySuccessEntity.getPayType() == 1) {
            String receiver = mTvReceiver.getText().toString().trim();
            if (TextUtils.isEmpty(receiver) || TextUtils.equals("请选择", receiver)) {
                mBtnSave.setEnabled(false);
                return;
            }
        }
        if (mPaySuccessEntity.getPayType() == 2 && mPaySuccessEntity.isAttention()) {
            String gzhName = mEtGzhName.getText().toString().trim();
            if (TextUtils.isEmpty(gzhName)) {
                mBtnSave.setEnabled(false);
                return;
            }
        }
        mBtnSave.setEnabled(true);
    }
}
